package com.zhiwei.gateway.service.impl;

import com.zhiwei.common.util.UuidUtil;
import com.zhiwei.gateway.dao.entity.UserInfo;
import com.zhiwei.gateway.service.CaptchaService;
import com.zhiwei.gateway.service.LoginService;
import com.zhiwei.gateway.service.UserInfoService;
import com.zhiwei.gateway.service.UserTokenService;
import com.zhiwei.gateway.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * function
 * author zhiwei.yang
 * time 2020-2-27-16:59
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private CaptchaService captchaService;
    @Autowired
    private UserTokenService userTokenService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public String userLogin(String username, String password, String serialNo, String captcha) {
        UserInfo userInfo = userInfoService.getUserInfoByName(username);
        if(null == userInfo){
            log.error("登陆用户名为空");
            return null;
        }
        if(!passwordEncoder.matches(password, userInfo.getPassword())){
            log.error("用户[{}]密码不匹配", username);
            return null;
        }
        if(!captchaService.matches(serialNo, captcha)){
            log.error("用户验证码[{}]不匹配", username);
            return null;
        }
        return userTokenService.createToken(username);
    }

    @Override
    public void userLogout(String token, String serialNo) {
        userTokenService.removeToken(token);
        captchaService.removeCaptcha(serialNo);
        Map<String,Object> tokenInfo = JwtUtil.parseJwtToken(token);
        String username = (String) tokenInfo.get(JwtUtil.NAME);
        log.info("用户【{}】退出登陆, token失效 ==> {}", username, token);
    }

    @Override
    public String userRegister(String username, String password, String captcha, String serialNo) {
        if(!captchaService.matches(serialNo, captcha)){
            log.error("用户验证码[{}]不匹配", username);
            return StringUtils.EMPTY;
        }
        UserInfo userInfo = userInfoService.getUserInfoByName(username);
        if(null !=  userInfo){
            log.error("用户名称[{}]重复", username);
            return StringUtils.EMPTY;
        }
        UserInfo newUserInfo = new UserInfo();
        newUserInfo.setUuid(UuidUtil.getUUID());
        newUserInfo.setUsername(username);
        newUserInfo.setPassword(passwordEncoder.encode(password));
        Date nowTime = new Date();
        newUserInfo.setCreateTime(nowTime);
        newUserInfo.setUpdateTime(nowTime);
        newUserInfo.setCreateUser(username);
        newUserInfo.setUpdateUser(username);
        return userInfoService.addUser(newUserInfo) ? userTokenService.createToken(username) : StringUtils.EMPTY;
    }
}
